package com.hongdee.scm.common.service.impl;

import com.hongdee.common.util.TokenUtil;
import com.hongdee.scm.common.dao.ICommonDao;
import com.hongdee.scm.common.service.ICommonService;
import com.hongdee.scm.serialnumber.entity.TSerialNumber;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Mr. Xu on 2017/4/25.
 */
@Service
public class Common implements ICommonService{
    @Autowired
    private ICommonDao iCommonDao;


    //流水号表
    private String tabName = "T_Serial_Number";

    @Override
    public Boolean appendWhereIfNeed(StringBuffer sql, Boolean hasWhere) {

        if (hasWhere == false) {

            sql.append(" where ");

        } else {

            sql.append(" and ");

        }
        return true;

    }

    /**
     * 公司下仓库下的车辆
     */
    public List carOfCompany(String uid) throws Exception {
        return this.iCommonDao.list("SELECT * FROM T_SCM_STOCK where warehouse_id in (select id from T_SCM_WAREHOUSE where company_id = ? )", TokenUtil.getCompanyId(uid));
    }

    /**
     * 根据日期获取流水号，若没有新生成流水号0001
     *
     * @throws Exception
     */
    @Override
    public String serialNumber(String type) throws Exception {
        Map map = new HashMap();
        List params = new ArrayList();
        StringBuffer sqlBuf = new StringBuffer();
        String currentTime = this.currentTime();
        sqlBuf.append("select * from " + tabName + " where serial_time = ? ");
        params.add(currentTime);
        if (type != null && !type.equals("")) {
            sqlBuf.append(" and type = ? ");
            params.add(type);

            map = this.iCommonDao.info(sqlBuf.toString(),params.toArray());
        } else {
            throw new Exception("请选择类型！");
        }
        String serial = "0001";
        if (map.size()>0) {
            if (map.get("serialNumber") == null || StringUtils.isEmpty(map.get("serialNumber"))) {
                throw new Exception("未查询到流水号！");
            } else{
                serial=String.valueOf(map.get("serialNumber"));
            }
        } else {
            //插入新流水号
            TSerialNumber entity = new TSerialNumber();
            entity.setSerialNumber("0001");
            entity.setType("0");
            entity.setSerialTime(currentTime);
            this.iCommonDao.saveOrUpdate(entity);

        }
        this.updateSerialNumber(serial);
        //新流水号
        return serial;
    }
    //更新流水号
    @Override
    public void updateSerialNumber (String currentSerial) throws Exception{
        Long s = Long.parseLong(currentSerial);
        s=++s;
        s=s==10000?1:s;
        String reslut=s>10?(s>100?(s>1000?s+"":"0"+s):"00"+s):"000"+s;
        this.iCommonDao .executeSQL("update "+tabName+ " set serial_number = ? where serial_time = ? ",reslut,this.currentTime());
    }
    //获取当前时间，格式为“20170604”
    @Override
    public String currentTime() throws Exception {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }


    /**
     * 验证参数格式 符合返回true，反之false
     * @param format 标准格式
     * @param date 待验证日期字符
     * @return
     * @throws Exception
     * made by Miss_Jin
     */
    @Override
    public boolean verificationDate(String format,String date) throws Exception{
        SimpleDateFormat f = new SimpleDateFormat(format);
        boolean dateflag=true;
        try {
            Date d = f.parse(date);
        } catch (ParseException e) {
            dateflag = false;
        }
        return dateflag;
    }
}
